package com.example.statelayouthelper

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import cbfg.state.StateLayoutHelper
import com.example.statelayouthelper.databinding.ActivityDemo3Binding

/**
 * @Description: Builder2 Demo
 * @Author: TomHawk
 * @CreateDate: 2022/8/18 15:27
 */
class Demo3Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityDemo3Binding.inflate(layoutInflater)
        setContentView(binding.root)

        val statusLayoutHelper = StateLayoutHelper.Builder(binding.root)
            .addState(STATE_EMPTY, binding.vsEmpty)
            .addState(STATE_ERROR, binding.vsError)
            .addState(STATE_NETWORK_ERROR, binding.vsNetError)
            .setStateClickListener { _, state, view ->
                Toast.makeText(
                    this,
                    "Click state:$state,viewId:${view.id}",
                    Toast.LENGTH_SHORT
                ).show()
            }
            .build()

        binding.btnEmpty.setOnClickListener {
            statusLayoutHelper.showState(STATE_EMPTY)
        }

        binding.btnError.setOnClickListener {
            statusLayoutHelper.showState(STATE_ERROR)
        }

        binding.btnNetworkError.setOnClickListener {
            statusLayoutHelper.showState(STATE_NETWORK_ERROR)
        }
    }

    companion object {
        private const val STATE_EMPTY = 1
        private const val STATE_ERROR = 2
        private const val STATE_NETWORK_ERROR = 3
    }
}